<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>高阶函数</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
</head>

<body>

    <div id="react"> </div>
    <!--准备好一个容器-->
    <div id="test"></div>
    <!--引入react核心库,核心库必须先引入-->
    <script type="text/javascript" src="./js/react.development.js"></script>
    <!--引入react-dom, 用于支持react操作DOM-->
    <script type="text/javascript" src="./js/react-dom.development.js"></script>
    <!--引入babel,用于将jsx转为js-->
    <script type="text/javascript" src="./js/babel.min.js"></script>

    <!-- 引入prop-types，用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="./js/prop-types.js"></script>

   <div id="react"></div>
    <script type="text/babel">
    class Login extends React.Component {

   //初始化状态
   state={username:'', password:''}

   getData=(datatest,e)=>{
    this.setState({[datatest]:e}) 
   }
   
   getSubmit=()=>{
     //原生阻止默认事件
     event.preventDefault()
     const {username,password} =this.state
     alert(`姓名${username},密码${password}`)
     }

      render() {
         return (
           <form onSubmit={this.getSubmit}>
                {/*这块this.getData("username")加了（），onChange把getData函数的返回值作为回调，不加（），会把getData函数本身作为回调*/}
            姓名:<input onChange={(event)=>{return this.getData("username",event.target.value)}} type="text" name="username"/>
                 <h2>{this.state.username}</h2>
            密码:<input onChange={event=>this.getData("password",event.target.value)} type="password" name="password"/>
                <h2>{this.state.password}</h2>
                <button>登录</button>
           </form>
         )
      }

    }
   ReactDOM.render(<Login/>,document.getElementById("react"))
    </script>
    <style>
        .spacing {
            margin: 15px;
        }
    </style>

</body>

</html>